
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *

from ui.control import Ui_Form

from common.libs.logger import logger
import qtawesome as qta


class ControlWidget(QWidget, Ui_Form):

    device = {}
    online_status = {0: "离线", 1: "在线"}
    infrared_status = {0: "无人", 1: "有人"}
    door_status = {0: "关闭", 1: "开启"}
    switch_status = {0: "关闭", 1: "开启"}
    lock_status = {0: "禁止出源", 1: "可以出源"}

    def __init__(self):
        super(ControlWidget, self).__init__()
        self.setupUi(self)

    def setDevice(self, device):
        self.device = device
        self.updateInfo()

    def setOnline(self, online):
        if online == 1:
            self.label_online.setText(chr(0xf192) + ' ' + '在线')
            self.label_online.setFont(qta.font('fa', 16))
            self.label_online.setStyleSheet("color:#00ff00;")
        else:
            self.label_online.setText(chr(0xf192) + ' ' + '离线')
            self.label_online.setFont(qta.font('fa', 16))
            self.label_online.setStyleSheet("color:red;")

    def updateInfo(self):
        device = self.device
        logger.info(device)

        self.groupBox_device.setTitle(device['name'] + '-' + device['position'])
        self.setOnline(device['online'])
        # self.label_online.setText(self.online_status[device['online']])
        # self.label_position.setText(device['position'])

        self.label_temperature.setText('%.2f' % device['environment']['temperature'])
        self.label_humidity.setText('%.2f' % device['environment']['humidity'])
        self.label_press.setText('%.2f' % device['environment']['press'])
        self.label_ktp.setText('%.2f' % device['environment']['ktp'])

        self.label_infrared.setText(self.infrared_status[device['status']['infrared']])
        self.label_door.setText(self.door_status[device['status']['door']])
        self.label_switch.setText(self.switch_status[device['status']['door']])
        self.label_lock.setText(self.lock_status[device['status']['lock']])





